package com.example.gate.websocket;

import com.example.game.CResp;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;

import java.util.List;

/**
 * @author: Allen
 * @create: 2025/7/14 16:37
 * @description:
 **/
public class WSClientProtobufEncoder extends MessageToMessageEncoder<CResp> {


    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, CResp msg,  List<Object> out) throws Exception {
        byte[] data = msg.toByteArray();
        ByteBuf buffer = Unpooled.buffer(8 + data.length);
        buffer.writeInt(0);
        buffer.writeInt(data.length);
        buffer.writeBytes(data);
        // 创建BinaryWebSocketFrame
        out.add(new BinaryWebSocketFrame(buffer));

    }
}
